<template>
  <view>
    <!-- 收货地址盒子 -->
    <view v-if="JSON.stringify(address)=== '{}'" class="address-choose-box">
      <button @click="chooseAddress" type="primary" size="mini" class="address-choose-btn">请选择收货地址+</button>
    </view>
    <!-- 收货信息 -->
    <view v-else class="adress-info-box">
      <view class="row-1">
        <view class="row-1-left">
          收货人：{{address.userName}}
        </view>
        <view class="row-1-right">
          <text>电话：{{address.telNumber}}</text>
          <uni-icons @click="chooseAddress" type="right"></uni-icons>
        </view>
      </view>
      <view class="row-2">
        <view class="row-2-left">
          收货地址：
        </view>
        <view class="row-2-right">
          {{addstr}}
        </view>
      </view>
    </view>
    <!-- 底部边框图标 -->
    <image src="/static/cart_border@2x.png" class="address-border" mode=""></image>
  </view>
</template>

<script>
  // 导入两个辅助函数
  import { mapState, mapMutations, mapGetters } from 'vuex'
  export default {
    name: "my-address",
    data() {
      return {
        // address: {}
      };
    },
    methods: {
      // 映射更新地址的方法
      ...mapMutations('m_user', ['updateAdress']),
      async chooseAddress() {
        if (!uni.chooseAddress) return uni.$showMsg('仅在小程序中可以设置')
        const [err, sus] = await uni.chooseAddress().catch()
        console.log(sus);
        if (err === null && sus.errMsg === "chooseAddress:ok") {
          // 更新地址
          this.updateAdress(sus)
        }
      }
    },
    computed: {
      // 映射vuex中地址数据
      ...mapState('m_user', ['address']),
      // 使用vuex中的计算属性
      ...mapGetters('m_user', ['addstr'])
    }
  }
</script>

<style lang="scss">
  .address-border {
    height: 20rpx;
    width: 100%;
  }

  .address-choose-box {
    height: 180rpx;
    display: flex;
    justify-content: center;
    align-items: center;

    .address-choose-btn {}
  }

  .adress-info-box {
    display: flex;
    flex-direction: column;
    justify-content: center;
    font-size: 24rpx;
    padding: 10rpx;

    .row-1 {
      display: flex;
      justify-content: space-between;
      margin-top: 20rpx;

      .row-1-left {}

      .row-1-right {
        display: flex;
      }
    }

    .row-2 {
      display: flex;
      align-items: center;
      margin-top: 20rpx;

      .row-2-left {
        white-space: nowrap;
      }

      .row-2-right {}
    }
  }
</style>
